package com.nju.mini_tiktok;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

import com.nju.mini_tiktok.adapter.ViewPagerAdapter;
import com.nju.mini_tiktok.bean.DataCreate;
import com.nju.mini_tiktok.entity.ShortVideoInfo;
import com.nju.mini_tiktok.holder.RecyclerItemNormalHolder;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class PlayActivity extends AppCompatActivity {

    protected Unbinder unbinder;
    @BindView(R.id.view_pager2)
    ViewPager2 viewPager2;
    public static int initPos = 0;
    private List<ShortVideoInfo> mList = new ArrayList<>();
    private ViewPagerAdapter viewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        unbinder = ButterKnife.bind(this);
        getData();
    }

    private void getData() {

//        setData();
        viewPagerAdapter = new ViewPagerAdapter(this, DataCreate.mList);
        viewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
        viewPager2.setAdapter(viewPagerAdapter);

        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                // 大于0说明有播放
                int playPosition = GSYVideoManager.instance().getPlayPosition();
                if (playPosition >= 0) {
                    // 对应的播放列表TAG
                    if (GSYVideoManager.instance().getPlayTag().equals(RecyclerItemNormalHolder.TAG)
                            && (position != playPosition)) {
                        playPosition(position);
                    }
                }
            }
        });
        viewPager2.post(new Runnable() {
            @Override
            public void run() {
                playPosition(initPos);
            }
        });

    }

    private void playPosition(int position) {
        RecyclerView.ViewHolder viewHolder = ((RecyclerView) viewPager2.getChildAt(0)).findViewHolderForAdapterPosition(position);
        if (viewHolder != null) {
            RecyclerItemNormalHolder recyclerItemNormalHolder = (RecyclerItemNormalHolder) viewHolder;
            recyclerItemNormalHolder.getPlayer().startPlayLogic();
        }
    }

//    /**
//     * 模拟数据
//     */
//    private void setData() {
//

//
//        ShortVideoInfo data2 = new ShortVideoInfo();
//        data2.setTextContent("这是一条一起学猫叫的视频");
//        data2.setAuthor("LILILI");
//        data2.setVideoCover("http://8.136.101.204/v/%E9%A5%BA%E5%AD%90%E8%BF%98%E5%B9%B4%E8%BD%BB.jpg");
//        data2.setVideoUrl("http://8.136.101.204/v/%E9%A5%BA%E5%AD%90%E8%BF%98%E5%B9%B4%E8%BD%BB.mp4");
//        mList.add(data2);
//
//        ShortVideoInfo data3 = new ShortVideoInfo();
//        data3.setTextContent("赶紧把这个转发给你们的女朋友吧，这才是对她们最负责的AI");
//        data3.setAuthor("新闻启示录");
//        data3.setVideoCover("http://8.136.101.204/v/%E9%A5%BA%E5%AD%90%E6%8C%BA%E4%BD%8F.jpg");
//        data3.setVideoUrl("http://8.136.101.204/v/%E9%A5%BA%E5%AD%90%E6%8C%BA%E4%BD%8F.mp4");
//        mList.add(data3);
//
//        ShortVideoInfo data4 = new ShortVideoInfo();
//        data4.setTextContent("男明星身高暴击");
//        data4.setAuthor("综艺大咖秀");
//        data4.setVideoCover("http://8.136.101.204/v/%E9%A5%BA%E5%AD%90%E5%8F%AF%E4%BB%A5.jpg");
//        data4.setVideoUrl("http://8.136.101.204/v/%E9%A5%BA%E5%AD%90%E5%8F%AF%E4%BB%A5.mp4");
//        mList.add(data4);
//
//        ShortVideoInfo data5 = new ShortVideoInfo();
//        data5.setTextContent("挑战手抓饼的一百种吃法第七天");
//        data5.setAuthor("南翔不爱吃饭");
//        data5.setVideoCover("http://8.136.101.204/v/%E9%A5%BA%E5%AD%90%E6%83%B3%E5%90%AC.jpg");
//        data5.setVideoUrl("http://8.136.101.204/v/%E9%A5%BA%E5%AD%90%E6%83%B3%E5%90%AC.mp4");
//        mList.add(data5);
//
//    }
}